import sqlCore from "../mysql";
import Utils from "../utils";
class UserModel {
  //用户登录
  async userloginModel(ctxBody: any) {
    return await sqlCore.allSqlAction(
      `SELECT * FROM user WHERE chat_num = '${ctxBody.chatNum}' AND password = '${ctxBody.password}'`
    );
  }

  //获取用户好友
  async getUserFriendsModel(ctxBody: any) {
    return await sqlCore.allSqlAction(
      `select * from user_friends where user_id_a = "${ctxBody.userId}" or user_id_b = "${ctxBody.userId}"`
    );
  }

  // 添加用户
  async addUser(form: any) {
    // 必选列
    const requiredColumns = ["chat_num", "nick_name", "password"];

    // 所有列
    const allColumns = [
      ...requiredColumns,
      "sex",
      "avatar",
      "intro",
      "bg_img",
      "created_time",
    ];

    // 获取有效值
    const validValues = await Utils.getValidValues(form, allColumns);

    // 创建一个包含列名和对应值的对象数组
    const columnValuePairs = allColumns.reduce((acc, column, index) => {
      acc.push({ column, value: validValues[index] });
      return acc;
    }, []);

    // 构建 SQL 语句
    const sql = `INSERT INTO user (${columnValuePairs
      .map((pair) => pair.column)
      .join(", ")}) VALUES (${columnValuePairs
      .map((pair) => (pair.value === "DEFAULT" ? "DEFAULT" : `"${pair.value}"`))
      .join(", ")})`;

    return await sqlCore.allSqlAction(sql);
  }
  //获取用户
  async getUser(ctxBody: any) {
    let sql = `select * from user`;

    const conditions = [];
    if (ctxBody) {
      if (ctxBody.id) {
        conditions.push(`id = "${ctxBody.id}"`);
      }
      if (ctxBody.chatNum) {
        conditions.push(`chat_num = "${ctxBody.chatNum}"`);
      }
      if (ctxBody.nickName) {
        conditions.push(`nick_name LIKE "%${ctxBody.nickName}%"`); // 模糊查询
      }
      if (ctxBody.sex) {
        conditions.push(`sex = "${ctxBody.sex}"`);
      }
    }
    if (conditions.length > 0) {
      sql += " WHERE " + conditions.join(" AND ");
    }

    sql += ` LIMIT ${(ctxBody.pageNum - 1) * ctxBody.pageSize},${
      ctxBody.pageNum * ctxBody.pageSize
    }`;

    return await sqlCore.allSqlAction(sql);
  }
  // 修改用户
  async updateUser(form: any) {
    const conditions = [];

    if (form.name) {
      conditions.push(`name = "${form.name}"`);
    }
    if (form.password) {
      conditions.push(`password = "${form.password}"`);
    }
    if (form.sex) {
      conditions.push(`sex = "${form.sex}"`);
    }
    if (form.avatar) {
      conditions.push(`avatar = "${form.avatar}"`);
    }
    if (form.intro) {
      conditions.push(`intro = "${form.intro}"`);
    }
    if (form.bg_img) {
      conditions.push(`bg_img = "${form.bgImg}"`);
    }

    if (conditions.length === 0) {
      // 没有值更新
      return Promise.resolve("没有更新");
    }

    const sql = `UPDATE user SET ${conditions.join(", ")} WHERE id = ${
      form.id
    }`;
    return await sqlCore.allSqlAction(sql);
  }
  // 删除用户
  async deleteUser(userId: number) {
    const sql = `DELETE FROM user WHERE id = ${userId}`;
    return await sqlCore.allSqlAction(sql);
  }
}
export default new UserModel();
